package com.suoluo.zenadmin.service;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;

import com.suoluo.zenadmin.dao.RequestLogDAO;
import com.suoluo.zenadmin.dao.hotel.HotelDAO;
import com.suoluo.zenadmin.dao.news.NewsDAO;
import com.suoluo.zenadmin.vo.RequestLog;

public class TaskExecutorImpl implements TaskExecutor {

	@Autowired
	private NewsDAO newsDAO;

	@Autowired
	private HotelDAO hotelDAO;
	
	@Autowired
	private RequestLogDAO requestLogDAO;

	@Resource(name = "executor")
	private ThreadPoolTaskExecutor executor;

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void submitViewNews(final int newsId) {
		ThreadPoolTaskExecutor.Task task = new ThreadPoolTaskExecutor.Task() {
			
			@Override
			public void run() {
				newsDAO.updateIncrementNewsViewnum(newsId);
			}
		};
		
		task.setTaskName("update new viewnums");
		task.setParams("newsId="+newsId);
		executor.execute(task);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void submitViewHotel(final int hotelId) {

		ThreadPoolTaskExecutor.Task task = new ThreadPoolTaskExecutor.Task() {

			@Override
			public void run() {
				hotelDAO.updateIncrementHotelViewnum(hotelId);
			}
		};

		task.setTaskName("update hotel viewnums");
		task.setParams("hotelId=" + hotelId);
		executor.execute(task);
	}

	@Override
	public void addRequestLog(final RequestLog log) {
		ThreadPoolTaskExecutor.Task task = new ThreadPoolTaskExecutor.Task() {

			@Override
			public void run() {
				requestLogDAO.addRequestLog(log);
			}
		};
		task.setTaskName("add request log");
		task.setParams("request_url=" + log.getUrl());
		executor.execute(task);
	}
	
	
}
